﻿Imports System.Text
Imports VsSpeakerCompanion
Imports System.ComponentModel.Composition
Imports System.IO

Namespace SoundSnippetWidget
	''' <summary>
	''' Allows the speaker to play three different sounds at any moment by clicking on any of the three provided buttons
	''' </summary>
	Public Class Widget
		Implements IWidget
		' our UI
        Private WithEvents soundSnippet As SoundSnippet
		' core context to access Demo Dashboard settings
		Private context As Context

		Public Sub New(ByVal context As Context)
			Me.context = context

            soundSnippet = New SoundSnippet
			soundSnippet.DataContext = Me
        End Sub

        Public ReadOnly Property Sound1Description As String
            Get
                Return GetSoundDescription("Sound1")
            End Get
        End Property

        Public ReadOnly Property Sound2Description As String
            Get
                Return GetSoundDescription("Sound2")
            End Get
        End Property

        Public ReadOnly Property Sound3Description As String
            Get
                Return GetSoundDescription("Sound3")
            End Get
        End Property

		Private Function GetSoundDescription(ByVal settingSoundKey As String) As String
            Dim soundFile = GetSoundFullPath(context.Settings.Get(Of String)(settingSoundKey))
			Return If(String.IsNullOrEmpty(soundFile), "default", Path.GetFileNameWithoutExtension(soundFile))
		End Function

        Private Sub soundSnippet_PlaySound1Clicked() Handles soundSnippet.PlaySound1Clicked
            PlaySound(context.Settings.Get(Of String)("Sound1"))
        End Sub

        Private Sub soundSnippet_PlaySound2Clicked() Handles soundSnippet.PlaySound2Clicked
            PlaySound(context.Settings.Get(Of String)("Sound2"))
        End Sub

        Private Sub soundSnippet_PlaySound3Clicked() Handles soundSnippet.PlaySound3Clicked
            PlaySound(context.Settings.Get(Of String)("Sound3"))
        End Sub

		Private Sub PlaySound(ByVal settingSoundKey As String)
			Try
                Dim configuredSoundPath = GetSoundFullPath(settingSoundKey)

				' check the sound file exists
				If File.Exists(configuredSoundPath) Then
					Dim sound As New System.Media.SoundPlayer(configuredSoundPath)
					sound.Play()
				Else
					' if the specified sound file doesn't exists
					' play the default (embedded) sound
					' If you want to change the default sound look into the Resources folder
                    Using defaultSound = SoundSnippetWidget.Resources.SoundFile.DefaultSound
                        Dim sound As New System.Media.SoundPlayer(defaultSound)
                        sound.Play()
                    End Using
				End If
			Catch
			End Try
		End Sub

		Private Function GetSoundFullPath(ByVal sound As String) As String
			Try
                If Not String.IsNullOrEmpty(sound) Then
                    If File.Exists(sound) Then
                        Return sound
                    ElseIf (Not Path.IsPathRooted(sound)) Then
                        Dim soundInTheSettingsPath = Path.Combine(Path.GetDirectoryName(Me.context.Settings.SettingsPath), sound)

                        If File.Exists(soundInTheSettingsPath) Then
                            Return soundInTheSettingsPath
                        End If
                    End If
                End If
			Catch
				Return sound
			End Try

			Return sound
		End Function

        Public ReadOnly Property VisualElement As System.Windows.UIElement Implements IWidget.VisualElement
            Get
                ' this is our UI
                Return soundSnippet
            End Get
        End Property

        Public Sub Reset() Implements IWidget.Reset
            ' this widget doesn't use any data that needs to be reset
        End Sub
	End Class
End Namespace
